
import React, { useEffect, useState } from 'react'
import { Button, Card, Form, Input, message } from 'antd';
import style from "../index.module.css"
import { useNavigate, useParams } from 'react-router-dom';
import { addHospitalRequest, getHospitalSetByIdRequest, updateHospitalSetByIdRequest } from '@/api/hospital/hospitalSet';
import { addHospitalParamType } from "@/api/hospital/model/hospitalSetType"


export default function HospitalAdd() {

    const navigate = useNavigate()
    const { id } = useParams()
    const [form] = Form.useForm()
    useEffect(() => {
        if (id) {
            const getHospitalSetById = async () => {
                const result = await getHospitalSetByIdRequest(id)
                console.log(result);

                form.setFieldsValue(result)
            }
            getHospitalSetById()
        }
    }, [])

    const addHospitalSetHandle = async (FieldsValue: addHospitalParamType) => {
        if (id) {
            await updateHospitalSetByIdRequest({ ...FieldsValue, id })
            navigate("/syt/hospital/hospitalset")
            message.success("修改成功")
        } else {
            await addHospitalRequest(FieldsValue)
            navigate("/syt/hospital/hospitalset")
            message.success("添加成功")
        }
    }
    const goBackHandle = () => {
        navigate(-1)
    }
    return (
        <Card>
            <Form
                form={form}
                name="basic"
                labelCol={{ span: 2 }}
                wrapperCol={{ span: 22 }}
                onFinish={addHospitalSetHandle}
            >
                <Form.Item label="医院名称:" name="hosname"
                    rules={[{ required: true, message: '请输入医院名称!', transform(value) { return value.trim() } }]}>
                    <Input />
                </Form.Item>

                <Form.Item label="医院编号:" name="hoscode"
                    rules={[{ required: true, message: '请输入医院编号!', transform(value) { return value.trim() } }]}>
                    <Input />
                </Form.Item>
                <Form.Item label="api基础路径:" name="apiUrl"
                    rules={[{ required: true, message: '请输入api基础路径!', transform(value) { return value.trim() } }]}>
                    <Input />
                </Form.Item>
                <Form.Item label="联系人姓名:" name="contactsName"
                    rules={[{ required: true, message: '请输入联系人姓名!', transform(value) { return value.trim() } }]}>
                    <Input />
                </Form.Item>
                <Form.Item label="联系人手机:" name="contactsPhone"
                    rules={[{ required: true, message: '请输入联系人手机!', transform(value) { return value.trim() } }]}>
                    <Input />
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 2 }}>
                    <Button type="primary" htmlType="submit" className={style.saveBtn}>
                        保存
                    </Button>
                    <Button type="default" onClick={goBackHandle}>
                        返回
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    )
}
